package business_logic.game_elements;


import java.util.Collection;
import java.util.EnumSet;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;


import model.collisions.CollisionCategory;
import model.elements.Explotion;
import model.elements.attributes.Coordinate2D;
import model.elements.attributes.Damage;
import model.elements.states.movement.ElementMovementState;
import model.exceptions.ModelExceptions;
import business_logic.GameElement;
import business_logic.Tile;
import business_logic.view.GameViewIdentifier;

public class Explosion extends Explotion implements GameElement
{
	
	private static double EXPLOSION_SPEED = 0.04;

    private static final Collection<GameViewIdentifier> m_viewIdentifiers = 
            EnumSet.of(GameViewIdentifier.SPRITE_EXPLOSION);
    
	public Explosion(Coordinate2D position, ElementMovementState direction, CollisionCategory type, Damage damage, int blastRange ) throws ModelExceptions
	{
		super( Coordinate2D.add( position, new Coordinate2D(.5,.5) ), direction, type, damage, blastRange*Tile.speedToTickets( EXPLOSION_SPEED ) );
		super.setSpeed(EXPLOSION_SPEED);
	}

	@Override
	public void vivir()
	{
		try {
			this.update();
		} catch (ModelExceptions e) {
			e.printStackTrace();
		}
	}

    @Override
    public int getX()
    {
        return Tile.pixelToTile( m_currentPosition.getRealX() );
    }

    @Override
    public int getY()
    {
        return Tile.pixelToTile( m_currentPosition.getRealY() );
    }

    @Override
    public Collection<GameViewIdentifier> getViewIdentifiers()
    {
        return m_viewIdentifiers;
    }

    private static final long m_serialVersionUID = 1L;
    
    @Override
    public Element save()
    {
        Element elem = DocumentHelper.createElement( this.getClass().getName() );
        elem.addAttribute( "serialVersion", m_serialVersionUID+"" );
        elem.addAttribute( "x", Math.floor( this.getPosition().getRealX() ) +"" );
        elem.addAttribute( "y", Math.floor( this.getPosition().getRealY() ) +"" );
        
        // Direccion
        elem.addAttribute( "direction", this.getDirection().getClass().getName()+"" );
        
        // Type/Colision
        elem.addAttribute( "type", this.getCollisionCategory().toString()+"" );
        
        // Damage
        elem.addAttribute( "damage", this.doDamage().getDamageValue()+"" );
        
        // TTL
        elem.addAttribute( "ttl", this.getTTL().getValue()+"" );
        
        return elem;
    }

    // Coordinate2D position, ElementMovementState direction, CollisionCategory type, Damage damage, int blastRange
    public Explosion( Element elem ) throws ModelExceptions, NumberFormatException, InstantiationException, IllegalAccessException, ClassNotFoundException
    {
        super
        (
            new Coordinate2D
            (
                Double.parseDouble( elem.attributeValue("x") ),
                Double.parseDouble( elem.attributeValue("y") )
            ),
            (ElementMovementState) Class.forName( elem.attributeValue("direction") ).newInstance(), 
            CollisionCategory.valueOf( elem.attributeValue("type") ),
            new Damage( Integer.parseInt( elem.attributeValue("damage") ) ), 
            Integer.parseInt( elem.attributeValue("ttl") )
        );
        
        super.setSpeed( EXPLOSION_SPEED );
        
    }
}
